package com.kim.whereeat.service;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.kim.whereeat.model.StoreInfo;
import com.kim.whereeat.utils.SQLiteHelper;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 伟阳 on 2015/10/24.
 */
public class StoreInfoService {

    private SQLiteHelper helper;
    private static final String TABLENAME = "storeinfo";

    public StoreInfoService(Context context) {
        helper = new SQLiteHelper(context);
    }

    /**
     * 判断商店是否存在
     *
     * @param storeName 商店名
     * @return true存在/false不存在
     */
    public boolean exists(String storeName) {
        boolean flag = false;
        SQLiteDatabase dbReader = helper.getReadableDatabase();
        Cursor cursor = dbReader.query(TABLENAME, null, "store_name=?", new String[]{storeName}, null, null, null);
        flag = cursor.getColumnCount() > 0;
        cursor.close();
        return flag;
    }

    /**
     * 添加商店
     *
     * @param storeInfo 商店信息
     * @return true添加成功/false添加失败
     */
    public boolean addStore(StoreInfo storeInfo) {
        SQLiteDatabase dbWriter = helper.getWritableDatabase();
        ContentValues cv = new ContentValues();
        cv.put("user_id", storeInfo.getUser_id());
        cv.put("store_name", storeInfo.getStore_name());
        cv.put("store_introduction", storeInfo.getStore_introduction());
        cv.put("store_image", storeInfo.getStore_image());
        cv.put("store_address", storeInfo.getStore_address());
        cv.put("store_phone", storeInfo.getStore_phone());
        cv.put("store_business_hours_start", storeInfo.getStore_business_hours_start());
        cv.put("store_business_hours_end", storeInfo.getStore_business_hours_end());
        long temp = dbWriter.insert(TABLENAME, null, cv);
        return temp > 0;
    }

    /**
     * 通过ID获取商店信息
     *
     * @param store_id 商店ID
     * @return null/StoreInfo
     * @throws InstantiationException
     * @throws IllegalAccessException
     * @throws NoSuchFieldException
     */
    public StoreInfo getStoreInfoById(int store_id) throws InstantiationException, IllegalAccessException, NoSuchFieldException {
        StoreInfo storeInfo = null;
        SQLiteDatabase dbReader = helper.getReadableDatabase();
        Cursor cursor = dbReader.query(TABLENAME, null, "id=?", new String[]{store_id + ""}, null, null, null);
        while (cursor.moveToNext()) {
            storeInfo = new StoreInfo();
            storeInfo.setId(cursor.getInt(0));
            storeInfo.setUser_id(cursor.getInt(1));
            storeInfo.setStore_name(cursor.getString(2));
            storeInfo.setStore_introduction(cursor.getString(3));
            storeInfo.setStore_image(cursor.getString(4));
            storeInfo.setStore_address(cursor.getString(5));
            storeInfo.setStore_phone(cursor.getString(6));
            storeInfo.setStore_business_hours_start(cursor.getString(7));
            storeInfo.setStore_business_hours_end(cursor.getString(8));
        }
        cursor.close();
        return storeInfo;
    }

    /**
     * 获取所有
     *
     * @return List<StoreInfo>
     * @throws InstantiationException
     * @throws IllegalAccessException
     * @throws NoSuchFieldException
     */
    public List<StoreInfo> getStoreInfoList() throws InstantiationException, IllegalAccessException, NoSuchFieldException {
        List<StoreInfo> storeInfoList = new ArrayList<>();
        SQLiteDatabase dbReader = helper.getReadableDatabase();
        Cursor cursor = dbReader.query(TABLENAME, null, null, null, null, null, null);
        while (cursor.moveToNext()) {
            StoreInfo storeInfo = new StoreInfo();
            storeInfo.setId(cursor.getInt(0));
            storeInfo.setUser_id(cursor.getInt(1));
            storeInfo.setStore_name(cursor.getString(2));
            storeInfo.setStore_introduction(cursor.getString(3));
            storeInfo.setStore_image(cursor.getString(4));
            storeInfo.setStore_address(cursor.getString(5));
            storeInfo.setStore_phone(cursor.getString(6));
            storeInfo.setStore_business_hours_start(cursor.getString(7));
            storeInfo.setStore_business_hours_end(cursor.getString(8));
            storeInfoList.add(storeInfo);
        }
        cursor.close();
        return storeInfoList;
    }

    /**
     * 通过搜索商店名获取商店信息列表
     *
     * @param store_name 商店名
     * @return List<StoreInfo>
     * @throws InstantiationException
     * @throws IllegalAccessException
     * @throws NoSuchFieldException
     */
    public List<StoreInfo> getStoreInfoListByName(String store_name) throws InstantiationException, IllegalAccessException, NoSuchFieldException {
        List<StoreInfo> storeInfoList = new ArrayList<>();
        SQLiteDatabase dbReader = helper.getReadableDatabase();
        String new_store_name = "%";
        for (int i = 0; i < store_name.length(); i++) {
            new_store_name += store_name.charAt(i) + "%";
        }
        Cursor cursor = dbReader.query(TABLENAME, null, "store_name like ?", new String[]{new_store_name}, null, null, null);
        while (cursor.moveToNext()) {
            StoreInfo storeInfo = new StoreInfo();
            storeInfo.setId(cursor.getInt(0));
            storeInfo.setUser_id(cursor.getInt(1));
            storeInfo.setStore_name(cursor.getString(2));
            storeInfo.setStore_introduction(cursor.getString(3));
            storeInfo.setStore_image(cursor.getString(4));
            storeInfo.setStore_address(cursor.getString(5));
            storeInfo.setStore_phone(cursor.getString(6));
            storeInfo.setStore_business_hours_start(cursor.getString(7));
            storeInfo.setStore_business_hours_end(cursor.getString(8));
            storeInfoList.add(storeInfo);
        }
        cursor.close();
        return storeInfoList;
    }


    /**
     * 关闭数据库
     */
    public void close() {
        if (helper != null)
            helper.close();
    }
}
